﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class LayoutManager {

	private $includes;
	private $header;
	private $beginColumns;
	private $endColumns;
	private $contentColumns;
	private $footer;
	private $ncolumns;
	
	public function addInclude($type, $src)	{
		switch ($type) {
			case 'css':
			case 'stylesheet':
			case 'style':
				$this->includes[] = '<link type="text/css" href="styles/'.$src.'.css" rel="stylesheet" />';
				break;
			case 'js':
			case 'javascript':
			case 'scrpt':
				$this->includes[] = '<script src="scripts/'.$src.'" type="text/javascript"></script>';
				break;
		}
	}
	
	public function setHeader($view, $data = array()) {
		$this->header = array('view' => $view, 'data' => $data);
	}
	
	public function addColumn ($viewBegin, $viewEnd, $data = array()) {
		$this->beginColumns[] = array('view' => $viewBegin, 'data' => $data);
		$this->endColumns[] = array('view' => $viewEnd, 'data' => $data);
		$this->ncolumns++;
		return $this->ncolumns;
	}
	
	public function setColumnDelimiters($idColumn, $viewBegin, $viewEnd, $data = array()) {
		$this->beginColumns[$idColumn] = array('view' => $viewBegin, 'data' => $data);
		$this->endColumns[$idColumn] = array('view' => $viewEnd, 'data' => $data);
	}
	
	public function addToColumn($idColumn, $view, $data = array()) {
		if($idColumn <= $this->ncolumns)
			$this->contentColumns[$idColumn] = array('view' => $view, 'data' => $data);
	}
	
	public function setFooter($view, $data = array()) {
		$this->footer = array('view' => $view, 'data' => $data);
	}
	
	public function render() {
		$ci = &get_instance();
		$this->header['data']['includes'] = $this->includes;
		$ci->load->view($this->header['view'], $this->header['data']);
		
		for($i=0; $i < $this->ncolumns; ++$i) {
			$ci->load->view($this->beginColumns[$i]['view'], $this->beginColumns[$i]['data']);
			if(isset($this->contentColumns[$i]['view']))
				$ci->load->view($this->contentColumns[$i]['view'], $this->contentColumns[$i]['data']);
			$ci->load->view($this->endColumns[$i]['view'], $this->endColumns[$i]['data']);
		}
		
		$ci->load->view($this->footer['view'], $this->footer['data']);
	}
	
    public function __construct() {
		$this->ncolumns = 0;
    }
}

/* End of file LayoutManager.php */